package com.renjun.smweb.commons.componet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.TypeReference;
import com.renjun.smweb.commons.util.ResponseUtils;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.*;

/**
 * Created by huamin on 2017/4/6.
 */
@Component
public class RedisComponent {

    //默认本地
    public static String redisSource="local_";

    JedisPool pool;
    Jedis jedis;

    //获取redis
    public Jedis getRedisResource() throws SQLException {
        String redisSourcehost=redisSource+"host";
        String redisSourcepass=redisSource+"pass";
        String host = getRedisConfig("redis.properties", redisSourcehost);
        String pass = getRedisConfig("redis.properties", redisSourcepass);
        pool = new JedisPool(new JedisPoolConfig(), host);
        jedis = pool.getResource();
        if(!"".equals(pass)){
            jedis.auth(pass);
        }

        return jedis;
    }

    //获取redis.properties配置文件信息
    private String getRedisConfig(String wjName, String valueName) throws SQLException {
        Properties p = new Properties();
        InputStream is = InitComponent.class.getClassLoader().getResourceAsStream(wjName);
        try {
            p.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String value = p.getProperty(valueName);
        return value;
    }

    //添加list数据到redis
    public void setRedisValueByList(String key, List<Map<String, Object>> list) throws SQLException {
        Jedis je = getRedisResource();
        String value = transListToString(list);
        je.set(key, value);
    }

    //删除redis数据
    public void delRedisValue(String key) throws SQLException {
        Jedis je = getRedisResource();
        je.del(key);
    }

    //获得redis的list
    public List<Map<String, Object>> getRedisList(String key) throws SQLException {
        Jedis je = getRedisResource();
        String value = "";
//        try {
        value = je.get(key);
//        } catch (Exception e) {
//            System.out.println("redis出错");
//            e.printStackTrace();
//        }
        List<Map<String, Object>> list = transStringToList(value);
        return list;
    }

    //添加数据到redis
    public void setRedisValue(Jedis je, String key, String value) {
        je.set(key, value);
    }

    //获得redis的value
    public String getRedisValue(String key) throws SQLException {
        Jedis je = getRedisResource();
        String value = "";
//        try {
        value = je.get(key);
//        } catch (Exception e) {
//            System.out.println("redis出错");
//            e.printStackTrace();
//        }
        return value;
    }

    //list转String
    public String transListToString(List<Map<String, Object>> list) {
        JSONArray jsonArray = new JSONArray();
        for (int i = 0, size = list.size(); i < size; i++) {
            jsonArray.add(i, list.get(i));
        }
        return JSONArray.toJSONString(jsonArray);
    }

    //String 转list
    public List<Map<String, Object>> transStringToList(String str) {
        List<Map<String, Object>> result = new ArrayList<>();
        JSONArray jsonArray = JSONArray.parseArray(str);
        for (int i = 0, size = jsonArray.size(); i < size; i++) {
            Map<String, Object> tempMap = (Map<String, Object>) jsonArray.get(i);
            result.add(tempMap);
        }
        return result;
    }

}
